{% extends "smra_portal/portal_template.html" %} 
{% block page_id %}{% if currentvirt %}search{% else %}search-public{%endif %}{% endblock %}
{% load extras %} 

{% block main %}

<form action="" method="post">
	{% csrf_token %}

	<!--  Title for the page -->
    {% if currentvirt %}
	<div class="page_title">Collection #{{currentvirt.pk}} -
        {{currentvirt.name}}</div>
    {% else %}
    <div class="page_title"></div>
    {% endif %}
    
	<div class="schema_set">	
		<!-- search field buttons -->
		<div class="button_row">		
			<button type="submit" id="submit" name="search"
				title="Search the collection using on the search terms" 
				value="Search">Search
			</button>
			<button type="reset"
				title="Reset all fields back to initial values from previous search">Reset
			</button>
			<button type="button" value="Clear" onclick="clearForms()"
				title="Clear all fields on page">Clear
			</button>
			<span class="button_right">
            {% if currentvirt %}
			    <button type="submit" id="submit" name="savesubquery"
					title="Search the collection using on the search terms"
					value="Save Search Terms as Subquery">
					Save Search as a Subset
			    </button> 
			    <span class="button_item">	
				    <span>with name</span>
				    <span>
					    <input type ="text" id="subqueryname" name="subqueryname"></input>
				    </span>	
			    </span>
            {% endif %}
			</span>
		</div>

		<!-- Search Blocks and Fields -->
		<div class="schema_block">
			<div class="schema_header">
				<a href="javascript: toggle('basic_schema')">Basic Search</a>
			</div>
			<table class="schema_data schema_show" id="basic_schema">
				<tr class="schema_row_odd">
					<td class="schema_input">{{ descform.as_p }}</td>
				</tr>
			</table>
		</div>

	<!-- 	<div class="schema_block">
			<div class="schema_header">
				<a href="javascript: toggle('media_schema')">Media Type</a>
			</div>
			<table class="schema_data" id="media_schema">
				{% for f in mediaform %} 
				{% cycle '<tr class="schema_row_odd">'  '' '<tr class="schema_row_even">' '' %}
					<td class="schema_label">{{ f.label_tag }}</td>
					<td class="schema_input">{{f }}</td>						
				{% cycle ''  '</tr>' %}
				{% endfor %}
			</table>
		</div>
  -->

		<!--  media form fields -->
		{% for ns,name,desc, len,form in metadataforms %}
		
		{% if len > 0 %}
		<div class="schema_block">
			<div class="schema_header">
				<a href="javascript: toggle('schema_{{name}}')">{{desc}}</a>
			</div>
			<table class="schema_data" id="schema_{{name}}">
				{% for f in form %} 
				{% cycle '<tr class="schema_row_odd">'  '' '<tr class="schema_row_even">' '' %}
					<td class="schema_data_label">{{ f.label_tag }}</td> 
					<td class="schema_data_input">{{ f }}</td> 
				{% cycle ''  '</tr>' %}
				{% endfor %}
			</table>
		</div>
		{% endif %}
		{% endfor %}
	</div>
	
	<!--  Search Results  -->

	{% if search_results.object_list %} 
	<div class="schema_set">
		<!-- save button  -->
		<div class="button_row">
        {% if currentvirt %}
		<span class="button_left">
			<button type="button" value="All" onclick="selectAll('results_table')"
				title="Select All">All</button>
		</span>
		<span class="button_left">
			<button type="button" value="None" onclick="selectNone('results_table')"
				title="Select None">None</button>
		</span>
		<span class="button_right">
        
			<button type="submit" id="submit" name="savelist"
						title="Makes a subquery out of the selected search results"
						value="Save as a Subquery">Save Results as a Subset</button>
			<span class="button_item">	
				<span>with name</span>
				<span>
					<input type ="text" id="listname" name="listname"></input>
				</span>	
			</span>	
            {% endif %}
		</span>
		<div></div>
		</div>
		
		<table class="row_set" id="results_table">
		<tr>
            {% if currentvirt %}
			<th>Select</th>
            {% else %}
            <th></th>
            {% endif %}
			<th>Object</th>
			<th>View</th>
		</tr>
		{% for field_inc,field_not,field in resultsformdata %}
		<tr class="{% cycle 'row_odd' 'row_even' %}">
            {% if currentvirt %}
			<td class="row_small row_box">{{resultsform|fieldlookup:field_inc }}</td>
            {% else %}
            <td class="row_small row_box"></td>
            {% endif %} 
			<td class="row_field">{{ field.0 }}</td> 
			<td class="row_box"><a href="{% url details pk=field.1 %}">Details</a></td>
		</tr>
		{% endfor %}
		</table>
		
		<!-- pagination -->
		{% include "smra_portal/form_pagination.html" with records=search_results %}
								
	</div>

	{% endif %}	
	
{% endblock main %}

{% block messages %}
	{% if not resultcount > 0 %}
		<div class="warning"><span>No objects found!</span></div>
	{% endif %}
{% endblock messages %}


	<!-- ============== leftbar ================= -->

	{% block leftbar %}
 
    {% if currentvirt %}
	<h1>Actions</h1>
	<div class="button_row">
		<button type="submit" id="submit" name="save"
				title="Save the current state of the search as the collection"
				value="Save Collection">Update Collection
		</button>
	</div>
	
	<h1>Views</h1>
    {% if currentvirt %}
	<div class="button_row">
		<a href="{% url virtmeta currentvirt.pk %}">Details</a>
	</div>
    {% endif %}
	<div class="button_row">
		<a class="button_disabled" href="#">Objects</a>
	</div>

    {% endif %}
	<!--  subqueries -->
	{% if user.is_authenticated and currentvirt%}
		<h1>Collection Subsets</h1>
		<table class="row_set">
		<tr>
			<th>Sel</th>
			<th>Subset</th> 
			<th>Del</th> 
		</tr>
		{% for field_inc,field_del,field in subquery_formdata %}
		<tr class="{% cycle 'row_odd' 'row_even' %}">
			<td class="row_box">{{subquery_form|fieldlookup:field_inc}}</td> 
			<td class="button_left">		
				<a 	href="{% url virt currentvirt.pk %}?s={{field.1|urlencode}}" 
					title="({{field.2|truncate:200}})" >
					{% if field.0 %} {{field.0}} {% else %} #{{field.3}} {% endif %}	
				</a> 
			</td> 
			<td class="row_box">{{subquery_form|fieldlookup:field_del}}</td>
			
		</tr>
		{% endfor %}
		</table>
		
		<div class="button_row">	
			<button class="button_left" type="submit" id="submit" name="search"
				title="Search the collection using selected subsets" 
				value="Search">Search</button>	
				
			<button class="button_right" type="submit" id="submit" name="deletesubquery"
				title="Deletes the selected subsets" 
				value="Delete">Delete</button>
		</div>
		
		
	{% endif %}

 {% endblock leftbar %}


<!-- ============== rightbar ================= -->

{% block rightbar %}
	
	<!-- History list -->
	{% if historylist|length > 0 %}
	<h1>Previous Searches</h1>
	{% for history,historyref,timestamp in historylist %}
	{% if history %}
	<div class="button_row">
        {% if currentvirt %}
		<a href="{% url virt currentvirt.pk %}?s={{historyref|escape}}" 
            title='({{history|default:"-"}})'>{{timestamp}}</a>
        {% else %}
        <a href="{% url virt 0 %}?s={{historyref|escape}}" 
            title='({{history|default:"-"}})'>{{timestamp}}</a>
        {% endif %}
	</div>
	{% endif %}
	{% endfor %} {% endif %}
	<!-- end history list -->
	
</form>

{% endblock rightbar %}

